<template>
  <div class="playlist-comment">
    <div class="textarea-input">
      <el-input type="textarea" maxlength="140" show-word-limit :rows="4" placeholder="请输入内容" v-model="textarea">
      </el-input>
      <el-button :disabled="disabled" @click="getcomment" plain>评论</el-button>
    </div>
    <div class="hotcomment-title" v-if="hotcomment[0]">
      <h3>精彩评论</h3>
    </div>
    <div class="hotcomment">
      <comment @reply="reply" :detail="item" v-for="(item,index) of hotcomment" :key="index" />
    </div>
    <div class="comment-title">
      <h3>最新评论({{commentCount}})</h3>
    </div>
    <div class="comment">
      <comment :type="2" @reply="reply" :id="id" :detail="item" v-for="(item,index) of comment" :key="index" />
    </div>
    <div class="pageset" @click="commenterror()">
      <el-pagination disabled :page-size.sync="pageSize" :current-page.sync="curPage" layout="prev, pager, next"
        :total="commentCount" background></el-pagination>
    </div>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import { getcommentplaylistAPI } from "@/api/playlist.js";
import { sendcommentAPI } from "@/api/comment.js";
import comment from "@/components/comment";
export default {
  name: "playlist-comment",
  //import引入的组件需要注入到对象中才能使用
  components: { comment },
  props: {
    id: {
      require: true,
    },
    commentCount: { require: true },
  },
  data() {
    //这里存放数据
    return {
      textarea: "",
      comment: "",
      hotcomment: "",
      disabled: false,
      curPage: 1,
      pageSize: 30,
    };
  },
  //计算属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    reply(commentId) {
      // console.log(commentId);
      this.$prompt("输入回复内容", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(async ({ value }) => {
          if (value != null) {
            const data = await sendcommentAPI(2, 2, this.id, value, commentId);
            // console.log(data);
            if (data.data.code === 200) {
              this.textarea = "";
              this.disabled = false;
              this.$message({
                type: "success",
                message: "发送成功" + value,
              });
            }
            if (data.data.code === 400) {
              this.$message({
                type: "error",
                message: "发送失败",
              });
            }
          } else {
            this.$message({
              type: "error",
              message: "评论不能为空",
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入",
          });
        });
    },
    async getcommentplaylist() {
      try {
        const data = await getcommentplaylistAPI(this.id);
        // console.log(data.data);
        this.comment = data.data.comments;
        let hotcomment = data.data.hotComments;
        let limit = hotcomment.length - 10;
        hotcomment.splice(10, limit);
        this.hotcomment = hotcomment;
      } catch (error) {
        console.log(error + "获取评论列表失败");
      }
    },
    async getcomment() {
      try {
        this.disabled = true;
        const data = await sendcommentAPI(1, 2, this.id, this.textarea);
        // console.log(data);
        if (data.data.code === 200) {
          this.textarea = "";
          // this.$nextTick(() => {
          //   this.getcommentplaylist()
          //
          // })
          this.disabled = false;
        }
      } catch (error) {
        console.log(error);
      }
    },
    commenterror() {
      this.$message.error("更多精彩评论，请去APP端查看");
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.getcommentplaylist();
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style scoped lang='less'>
.playlist-comment {
  .hotcomment-title {
    margin-top: 20px;
    margin-bottom: 20px;
  }
  .comment-title {
    margin-top: 20px;
    margin-bottom: 20px;
  }
  .textarea-input {
    display: flex;
    justify-content: space-between;
    .el-textarea {
      margin-right: 30px;
      /deep/.el-textarea__inner {
        min-height: 99px;
        max-height: 99px;
      }
    }
  }
  .pageset {
    display: flex;
    justify-content: center;
    .el-pagination {
      margin-top: 50px;
      margin-bottom: 80px;
    }
  }
}
</style>